<template>
  <el-card>
    <div slot="header" class="clearfix">
      <span>销售合同</span>
    </div>
    <v-searchForm
      :form-data="filter_info"
      :form-title="formTitle"
      @search="search"
      @reset="reset"
    />

    <v-tableBtns
      :show-export="true"
      :table-title="exportTitle"
      :table-data="exportData"
    />

    <v-table
      :table-title="tableTitle"
      :table-data="tableData"
      :deletable="false"
      btn-class="btn"
      @sortChange="sortChange"
      @edit="edit"
    />
    <v-pagination
      :page-info="pageInfo"
      :get-table-list="getTableList"
    />

  </el-card>
</template>

<script>
import { mapGetters } from 'vuex'

export default {
  name: 'Home',
  data() {
    return {
      onFilter: false,
      formTitle: [
        {
          cn: '合同编号: ',
          en: '合同编号'
        },
        {
          cn: '名称: ',
          en: '客户名称',
          placeholder: '合同/客户名称'
        },
        {
          cn: '合同金额: ',
          en: '合同金额',
          type: 'range',
          dataName: { min: '合同金额起', max: '合同金额止' }
        },
        {
          cn: '签订人: ',
          en: '负责人'
        },
        {
          cn: '合同状态: ',
          en: '合同状态',
          type: 'select',
          list: [
            {
              label: '签约',
              value: '签约'
            },
            {
              label: '执行中',
              value: '执行中'
            },
            {
              label: '完毕',
              value: '完毕'
            },
            {
              label: '意外终止',
              value: '意外终止'
            }
          ]
        },
        {
          cn: '签约时间: ',
          en: 'created_at',
          type: 'time'
        }

      ],
      filter_info: {},
      tableTitle: [
        {
          cn: '合同名称',
          en: '合同名称',
          sortable:true
        },
        {
          cn: '合同编号',
          en: '合同编号',
          sortable:true
        },
        {
          cn: '客户名称',
          en: '客户名称',
        },
        {
          cn: '合同金额',
          en: '合同金额',
          sortable:true
        },
        {
          cn: '签订人',
          en: '签订人',
          sortable:true
        },
        {
          cn: '签订日期',
          en: '签订日期',
          sortable:true
        },
        {
          cn: '合同状态',
          en: '合同状态',
          sortable:true
        },
      ],
      tableData: [],

      // 排序
      sidx: '签订日期',
      sortKey: null,

      exportTitle:[],
      exportData:[],

      // f分页
      pageInfo: {
        pageNumber: 1,
        pageSize: 30,
        totalPage: 0,
        totalRow: 0
      }

    }
  },
  computed: {
    ...mapGetters([
      'name',
      'roles'
    ]),
    filter_infos() {
      const data = {}
      const origin = this.filter_info
      for (const key in origin) {
        if (!this.filter_info[key] && this.filter_info[key] !== 0) {
          this.$delete(this.filter_info, key)
          continue
        }
        if (key === 'created_at') {
          data.创建时间起 = origin[key][0]
          data.创建时间止 = origin[key][1]
        } else {
          data[key] = origin[key]
        }
      }
      if (!origin.created_at) {
        delete data.start_time
        delete data.End_time
      }
      return data
    }
  },
  activated() {
    this.getTableList()
  },
  methods: {
    getTableList() {
      const loading = this.$loading()

      const datas = {
        page: this.pageInfo.pageNumber, rows: this.pageInfo.pageSize,
        ...this.filter_infos,
        sidx: this.sidx,
        sortKey: this.sortKey,
        contractType: 2
      }
      this.$get('action/SalesContract/dataGrid', datas)
        .then(res => {
          let list
          if (res.data.list && res.data.list.length) {
            list = res.data.list.map((item, index) => {
              // item.received_money = 12
              return item
            })
          }
          this.pageInfo.totalPage = res.data.totalPage
          this.pageInfo.totalRow = res.data.totalRow
          this.tableData = list || []
            loading.close()
        })
        .catch(err => {
          loading.close()
          this.$message({
            message: '获取数据失败',
            type: 'error'
          })
        })
    },
    upload() {
      // 跳转到创建页面
      // this.$router.push({name: 'invoice_manage.application'})
    },
    // 搜索
    search() {
      this.pageInfo.pageNumber=1
      this.getTableList()
    },
    reset() {
      this.filter_info = {}
      this.getTableList()
    },
    edit(val) {
      this.$router.push({
        name: 'bargain_manage.sales.sales_edit',
        params: {
          details: val.row
        }
      })
    },
    onDelete() {

    },
    // 排序
    sortChange({ column, prop, order }) {
      console.log(column, prop, order)
      this.sidx = prop || '签订日期'
      this.sortKey = order === 'ascending' ? 'asc' : null
      this.getTableList()
    }

  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>

    .top-nav {
        padding: 0 55px 15px 0;
    }

    .line {
        text-align: center;
    }

    .grid-content {
        /*font-size: 20px;*/
        cursor: pointer;
        color: #45d1e1;

        .color {
            /*font-size: 20px;*/
            color: #45d1e1;
            font-weight: 600;
        }
    }

    .table-menu {
        /*padding-left: 30px;*/
        padding-bottom: 15px;
    }
    .block {
        padding-top: 15px;
    }
</style>

